package com.zhixin.fans.ks.ui.main.mutual;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.logger.Logger;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;
import com.zhixin.fans.ks.R;
import com.zhixin.fans.ks.api.CoupleApi;
import com.zhixin.fans.ks.api.res.MutualContent;
import com.zhixin.fans.ks.api.res.MutualDetail;
import com.zhixin.fans.ks.api.res.MutualTalk;
import com.zhixin.fans.ks.manager.UserInfoManager;
import com.zhixin.fans.ks.ui.base.BaseDaggerActivity;

import org.eenie.common.subscribes.ProgressSubscriber;
import org.eenie.common.ui.PicPreviewActivity;
import org.eenie.common.views.QMUIWindowInsetLinearLayout;
import org.eenie.common.views.TopBarLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;



public class MutualDetailActivity extends BaseDaggerActivity implements BaseQuickAdapter.OnItemClickListener {
    public static final String ROUTE_PATH = "/mutual/mutual_detail";
    @BindView(R.id.top_bar)
    TopBarLayout mTopBar;
    @BindView(R.id.tv_user_name)
    TextView mTvUserName;
    @BindView(R.id.tv_talk_count)
    TextView mTvTalkCount;
    @BindView(R.id.rv_mutual_talk)
    RecyclerView mRvMutualTalk;
    @BindView(R.id.edit_search)
    EditText mEditSearch;
    @BindView(R.id.btn_search)
    Button mBtnSearch;
    @BindView(R.id.fragment_container)
    QMUIWindowInsetLinearLayout mFragmentContainer;
    MutualTalkAdapter mMutualTalkAdapter;

    List<MutualTalk> mTalks = new ArrayList<>();


    MutualContent mMutualContent;


    CoupleApi mCoupleApi;

    UserInfoManager mUserInfoManager;
    @BindView(R.id.img_user_header)
    QMUIRadiusImageView mImgUserHeader;
    @BindView(R.id.tv_mutual_time)
    TextView mTvMutualTime;
    @BindView(R.id.tv_mutual_talk_count)
    TextView mTvMutualTalkCount;
    @BindView(R.id.tv_mutual_content)
    TextView mTvMutualContent;
    @BindView(R.id.rv_pics)
    RecyclerView mRvPics;


    @Override
    public int initContentView() {
        return R.layout.activity_mutual_detail;
    }

    @Override
    public void initInjector() {
        mMutualContent = JSON.parseObject(getIntent().getStringExtra("mutual_content"), MutualContent.class);
        mCoupleApi = getApplicationComponent().getApiManager().create(CoupleApi.class);
        mUserInfoManager = getCoupleAppComponent().getUserInfoManager();
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mRvMutualTalk.setLayoutManager(new LinearLayoutManager(mContext));
        mRvMutualTalk.setAdapter(mMutualTalkAdapter = new MutualTalkAdapter(mTalks));

        mRvPics.setLayoutManager(new GridLayoutManager(mContext, 3));

        fetchMutualDetail();
    }


    private void fetchMutualDetail() {
        mCoupleApi.fetchMutualDetail(mUserInfoManager.getValue().getU_id(), mMutualContent.getId())
                .subscribe(new ProgressSubscriber<MutualDetail>(mContext) {
                    @Override
                    public void onNext(MutualDetail mutualDetail) {
                        fillDetail(mutualDetail);
                    }

                    @Override
                    public void onFinish() {

                    }
                });
    }


    private void fillDetail(MutualDetail detail) {
        mTvUserName.setText(detail.getNickname());
        mTvMutualTime.setText(detail.getAdd_time());
        mTvMutualContent.setText(detail.getDesc());
        mTvMutualTalkCount.setText(detail.getDis_num());
        mTvTalkCount.setText(detail.getZan_num());
        Glide.with(mContext).asBitmap().load(detail.getHead_pic()).into(mImgUserHeader);

        mTalks.clear();
        mTalks.addAll(detail.getDis_list());
        mMutualTalkAdapter.notifyDataSetChanged();

        mRvPics.setVisibility(TextUtils.isEmpty(detail.getImg_url()) ? View.GONE : View.VISIBLE);

        mRvPics.setAdapter(new MutualImgAdapter(detail.getImg_url(), this));

    }


    private void sendMutualTalk(String content) {
        mCoupleApi.sendMutualTalk(mUserInfoManager.getValue().getU_id(), mMutualContent.getId(), content)
                .subscribe(new ProgressSubscriber<List<MutualTalk>>() {
                    @Override
                    public void onNext(List<MutualTalk> mutualTalks) {
                        Logger.e(JSON.toJSONString(mutualTalks));
                        mEditSearch.setText("");
                    }

                    @Override
                    public void onFinish() {
                        fetchMutualDetail();
                    }
                });
    }


    @OnClick(R.id.btn_search)
    public void sendTalk() {
        if (!TextUtils.isEmpty(mEditSearch.getText())) {
            sendMutualTalk(mEditSearch.getText().toString());
        } else {
            ToastUtils.showShort("请输入评论内容");
        }
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        String url = adapter.getItem(position).toString();
        PicPreviewActivity.start(mContext,url);
    }
}
